package com.zcp.todayheadline.db;


import android.util.Log;

import org.xutils.DbManager;
import org.xutils.x;

/**
 * Created by Administrator on 2017/1/9.
 */

public class DB {

    private DbManager db;

    private DB() {
        final DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                .setDbName("headline.db")//根据需要填写数据库名称
                .setDbVersion(3)//数据库版本号
                .setDbOpenListener(new DbManager.DbOpenListener() {//打开数据库
                    @Override
                    public void onDbOpened(DbManager db) {
                        // 开启WAL, 对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {//更新数据库
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        if (oldVersion == newVersion) {

                        }
                        // TODO: ...
                        // db.addColumn(...);//添加列
                        // db.dropTable(...);//删除表
                        // ...
                        // or
                        // db.dropDb();//删除数据库
                    }
                });
        db = x.getDb(daoConfig);

        Log.d("DBToday", "初始化DB");
    }

    private static class SingletonInstance {
        private static final DB INSTANCE = new DB();
    }

    public static DbManager db() {
        return SingletonInstance.INSTANCE.db;
    }
}
